//
//  GuoXueController.swift
//  DiZhenYuJing
//
//  Created by zhibao on 2024/9/23.
//

import UIKit
import SwiftSoup

class LiShiController: SecSuperController,UITableViewDelegate,UITableViewDataSource,DataChangeDelegate {
    
    fileprivate var dataArray = [[String:Any]]()
    fileprivate var showTableView:MYTableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.gk_navTitle = LocalizedString("历史")
        
        let twoBtn = UIButton(type: .custom)
        twoBtn.frame = CGRectMake(0, 0, pixw(p:80), pixw(p:60))
        twoBtn.addTarget(self, action: #selector(mapClick), for: .touchUpInside)
        twoBtn.setImage(UIImage(systemName: "mappin.and.ellipse"), for: .normal)
        let item = UIBarButtonItem(customView: twoBtn)
        self.gk_navRightBarButtonItems = [item]
        
        self.initTableView()
        self.dataArray = earthquakeData
        self.showTableView.reloadData()
        self.getListData()
    }
    
    @objc func mapClick(){
        
        let controller = LiShiMapController()
        controller.dataArray = self.dataArray
        self.navigationController?.pushViewController(controller, animated: true)
    }
    func initTableView() {
        showTableView = MYTableView(frame: CGRectMake(0, navigationFullHeight(), SCREEN_WIDTH, SCREEN_HEIGHT - navigationFullHeight() - tabBarFullHeight()), style: .plain)
        showTableView.backgroundColor = MYColor.color(withHexString: "f5f5f8")
        showTableView.delegate = self
        showTableView.dataSource = self
        showTableView.dataChangeDelegate = self
        showTableView.separatorStyle = .none
        showTableView.register(LiShiCell.self, forCellReuseIdentifier: "LiShiCell")
        self.view.addSubview(showTableView)
        showTableView.noDataType = .TableViewNoDataType_FILE
        showTableView.mj_footer?.endRefreshingWithNoMoreData()
    }
}

extension LiShiController {
    func getListData(){
        let paras = [String:String]()
//        HttpHelper.sharedInstance().sendCsiPost("https://www.csi.ac.cn/eportal/ui?pageId=54", parameters: paras) { tast, html in
//            QMUITips.hideAllTips()
//            self.parseHTMLWithSwiftSoup(html: html)
////            let controller = ResultDetailController()
////            controller.loadHtml = html
////            controller.type  = self.type
////            self.navigationController?.pushViewController(controller, animated: true)
//        } failure: { tast, error in
//            
//        }
//        return
        let times = TimeHelper.share().getCurrentTimeStr()
        DLog(message: "times==\(String(describing: times))")
        HttpHelper.sharedInstance().sendGet("ajax/google", parameters: ["rand":times ?? ""]) { task, model in
            DLog(message: "modell===\(String(describing: model.data))")
            self.dataArray = model.data as! [[String : Any]]
            self.showTableView.reloadData()
        } failure: { task, error in
            
        }
    }
    
    func parseHTMLWithSwiftSoup(html: String) {
        dataArray.removeAll()
        do {
            var tempArray = [[String:Any]]()
            let doc = try SwiftSoup.parse(html)
            // 查找所有的 <li> 标签
            let lis = try doc.select("li")
            var dataDic = [String:Any]()
            for li in lis.array() {
                var liHtml = ""
                let liClass = try li.attr("class")
                if liClass == "lsdz_list" {
                    liHtml = try li.outerHtml()
                    
                    let lidoc = try SwiftSoup.parse(liHtml)
                    // 查找所有的 <span> 标签
                    let spans = try lidoc.select("span")
                    for span in spans.array() {
                        let spanClass = try span.attr("class")
                        if spanClass == "span1" {
                            DLog(message: "span1==\(try span.text())")
                            dataDic["M"] = try span.text()
                        }
                        if spanClass == "span2" {
                            DLog(message: "span2==\(try span.text())")
                            dataDic["O_TIME"] = try span.text()
                        }
                        if spanClass == "span3" {
                            DLog(message: "span3==\(try span.text())")
                            dataDic["EPI_LON"] = try span.text()
                        }
                        if spanClass == "span4" {
                            DLog(message: "span4==\(try span.text())")
                            dataDic["EPI_LAT"] = try span.text()
                        }
                        if spanClass == "span5" {
                            DLog(message: "span5==\(try span.text())")
                            dataDic["EPI_DEPTH"] = try span.text()
                        }
                        if spanClass == "span6" {
                            DLog(message: "span6==\(try span.text())")
                            dataDic["LOCATION_C"] = try span.text()
                        }
                    }
                    
                    tempArray.append(dataDic)
                }
            }
            
            for i in 0 ..< tempArray.count {
                self.dataArray.insert(tempArray[i], at: 0)
            }
            
            DLog(message: "dataArray==\(self.dataArray)")
            
            self.showTableView.reloadData()
        } catch {
            print("Parsing error: \(error.localizedDescription)")
        }
    }
    
    func refreshData(withTabelViewTag tableViewTag: Int) {
        self.getListData()
    }
    
    func loadMoreData(withTabelViewTag tableViewTag: Int) {
        self.getListData()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let dic = dataArray[dataArray.count - indexPath.row - 1]
        let cell = tableView.dequeueReusableCell(withIdentifier: "LiShiCell") as! LiShiCell
        cell.updata(dic: dic)
        if MYGlobal.share().isVip() == false && indexPath.row < 3{
            cell.bgView.viewWithTag(blurEffectTag)?.isHidden = false
        }else{
            cell.bgView.viewWithTag(blurEffectTag)?.isHidden = true
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let flag = MYGlobal.share().isVip()
        if(flag == false){
            Helper.alertShow(withTitle: LocalizedString("提示"), withContent: LocalizedString("您不是VIP会员，暂时无法使用该功能！点击去开通解锁该功能！"), withSureBtnTitle: LocalizedString("去开通"), withCancelBtnTitle: LocalizedString("取消")) { index in
                //去往购买中心
                if index == 1 {
                    let controller = PayMarkController()
                    self.navigationController?.pushViewController(controller, animated: true)
                }
            }
            return
        }
        let dic = dataArray[dataArray.count - indexPath.row - 1]
        let controller = MapDetailController()
        controller.dic = dic
        self.navigationController?.pushViewController(controller, animated: true)
        
    }
}


